/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.bind.JAXBException;
import models.AccountBO;
import models.PageBO;
import models.PostBO;
import models.UserPageBO;
import myconfession.utils.ConstantProvider;

/**
 *
 * @author Liem
 */
public class MyServletContextListener implements ServletContextListener{

    public void contextInitialized(ServletContextEvent sce) {
        try {
            String webPath = sce.getServletContext().getRealPath("/");
            ResourceBundle resource = ResourceBundle.getBundle("constants");
            String accountXmlPath = webPath + resource.getString("accountXmlPath");
            String pageXmlPath = webPath + resource.getString("pageXmlPath");
            String userpageXmlPath = webPath + resource.getString("userpageXmlPath");
            String postXmlPath = webPath + resource.getString("postXmlPath");
            String appId = webPath + resource.getString("appId");
            String appSecret = webPath + resource.getString("appSecret");
            String fbAppUrl = webPath + resource.getString("fbApp");
            ConstantProvider cp = ConstantProvider.getInstance();
            cp.setAccountXmlPath(accountXmlPath);
            cp.setPageXmlPath(pageXmlPath);
            cp.setUserpageXmlPath(userpageXmlPath);
            cp.setPostXmlPath(postXmlPath);
            cp.setFbAppId(appId);
            cp.setFbAppSecret(appSecret);
            cp.setFbAppUrl(fbAppUrl);
            cp.setRootPath(webPath);
            AccountBO.loadAndMarshall(accountXmlPath);
            UserPageBO.loadAllAndMarshall(userpageXmlPath);
            PageBO.loadAllAndMarshall(pageXmlPath);
            PostBO.loadAllAndMarshall(postXmlPath);
        } catch (JAXBException ex) {
            Logger.getLogger(MyServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
    }
    
}
